package com.app.controller;

import com.app.model.ComRecruitersModel;
import com.app.model.CustomerModel;
import com.app.model.JobFollowModel;
import com.app.model.UserModel;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Controller
@RestController
public class JobFollowController {

    @Autowired
    public JobFollowService jobFollowService;
    @Autowired
    UserService userService;
    @Autowired
    private CustomerService customerService;

    @Autowired
    private JobPublishService jobPublishService;

    @Autowired
    ComRecruitersService comRecruitersService;

    @PostMapping("followAdd")
    public Result<Object> followAdd(
                            @RequestParam("follow") Integer follow,
                            @RequestParam("mobile") String mobile,
                            @RequestParam("followType") Integer followType,
                            HttpServletRequest request

    ){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String myMobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(myMobile);

        if (user == null) {
            return Result.error(405, "用户不存在");

        }
        if(followType == 1)
        {
            CustomerModel cus = customerService.getCustomerById(user.getCId());
            UserModel user2 = userService.getUserByMobile(mobile);
            ComRecruitersModel com = comRecruitersService.getComRecruitersById(user2.getRId());

            JobFollowModel jobFollow = new JobFollowModel();
            jobFollow.setCid(cus.getId());
            jobFollow.setRid(com.getId());
            jobFollow.setFollow(follow);
            jobFollow.setFollowType(followType);
            jobFollow.setCrtTime(CommonHelp.getNowTimestamp());
            int count =  jobFollowService.selectAllCount(jobFollow);
            if(count<=0)
            {
                jobFollowService.insert(jobFollow);
            }else {
               return Result.success("已存在");
            }
        } else if (followType == 2) {
            ComRecruitersModel com = comRecruitersService.getComRecruitersById(user.getRId());
            UserModel user2 = userService.getUserByMobile(mobile);
            CustomerModel cus = customerService.getCustomerById(user2.getCId());
            JobFollowModel jobFollow = new JobFollowModel();
            jobFollow.setCid(cus.getId());
            jobFollow.setRid(com.getId());
            jobFollow.setFollow(follow);
            jobFollow.setFollowType(followType);
            jobFollow.setCrtTime(CommonHelp.getNowTimestamp());
            int count =  jobFollowService.selectAllCount(jobFollow);
            if(count<=0)
            {
                jobFollowService.insert(jobFollow);
            }else {
                return Result.success("已存在");
            }
        }
        return Result.success(null);
    }
    //查询关注/收藏
    @RequestMapping("getFollowList")
    @ResponseBody
    public Result<Object> getFollowList(HttpServletRequest request,Integer type) throws ParseException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");

        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);

        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(jobFollowService.getFollowList(type,user));
    }
}
